﻿using System;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraTreeList.Nodes;
using ECP.Model;
using ECP.Service.Utilities;
using ECP.Utilities;

namespace ECP.Client.Permission
{
    public partial class PermissionSelectFrm : FormBase
    {
        private bool allowNull;
        private string byPermission;
        private bool checkMove;
        private string currentEntityID;
        private DataTable DTPermission;
        private bool IsUserClick;
        private bool multiSelect;
        private string openID;
        private string selectedFullName;
        private string selectedID;
        private string[] selectedIDs;
        private string SelecteTreeNodesFullName;
        private string SelecteTreeNodesID;

        public PermissionSelectFrm()
        {
            this.DTPermission = new DataTable(BasePermissionTable.TableName);
            this.selectedIDs = new string[0];
            this.byPermission = string.Empty;
            this.selectedID = string.Empty;
            this.selectedFullName = string.Empty;
            this.openID = string.Empty;
            this.currentEntityID = string.Empty;
            this.SelecteTreeNodesID = string.Empty;
            this.SelecteTreeNodesFullName = string.Empty;
            this.InitializeComponent();
        }

        public PermissionSelectFrm(string currentID)
            : this()
        {
            this.CurrentEntityID = currentID;
            this.OpenID = currentID;
        }

        public PermissionSelectFrm(string currentID, string parentID)
            : this()
        {
            this.OpenID = currentID;
            this.CurrentEntityID = parentID;
        }

        public event ButtonConfirmEventHandler OnButtonConfirmClick;

        private void BindData(bool reloadTree)
        {
            if (reloadTree)
            {
                this.LoadTree();
            }
            if ((this.tvPermission.FocusedNode == null) && (this.tvPermission.Nodes.Count > 0))
            {
                if (this.CurrentEntityID.Length == 0)
                {
                    this.tvPermission.FocusedNode = this.tvPermission.Nodes[0];
                }
                else
                {
                    BaseInterfaceLogic.FindTreeNode(this.tvPermission, this.CurrentEntityID, colParentID);
                    if (BaseInterfaceLogic.TargetNode != null)
                    {
                        this.tvPermission.FocusedNode = BaseInterfaceLogic.TargetNode;
                        BaseInterfaceLogic.ExpandTreeNode(this.tvPermission);
                    }
                }
                if (this.tvPermission.FocusedNode != null)
                {
                    this.tvPermission.FocusedNode.Expanded = true;
                }
            }
        }

        private bool CheckInput()
        {
            bool flag = false;
            if (this.tvPermission.FocusedNode != null)
            {
                flag = true;
            }
            if (!flag)
            {
                MessageBox.Show(AppMessage.MSG0023, AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            return flag;
        }

        private bool CheckInputMove()
        {
            bool flag = true;
            BaseInterfaceLogic.FindTreeNode(this.tvPermission, this.OpenID, colParentID);
            TreeListNode targetNode = BaseInterfaceLogic.TargetNode;
            BaseInterfaceLogic.FindTreeNode(this.tvPermission, this.CurrentEntityID, colParentID);
            TreeListNode targetTreeNode = BaseInterfaceLogic.TargetNode;
            if (!BaseInterfaceLogic.TreeNodeCanMoveTo(targetNode, targetTreeNode, colTreelist))
            {
                MessageBox.Show(AppMessage.Format(AppMessage.MSG0036, new string[] { targetNode.GetValue(colTreelist).ToString(), targetTreeNode.GetValue(colTreelist).ToString() }), AppMessage.MSG0000, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                flag = false;
            }
            return flag;
        }

        private void EntityToArray(TreeListNode TreeNode)
        {
            if ((TreeNode.GetValue(colParentID) != null) && TreeNode.Checked)
            {
                string str = TreeNode.GetValue(colParentID).ToString();
                this.SelecteTreeNodesID = this.SelecteTreeNodesID + str + ",";
            }
            for (int i = 0; i < TreeNode.Nodes.Count; i++)
            {
                this.EntityToArray(TreeNode.Nodes[i]);
            }
        }

        private void FormOnLoad()
        {
            this.IsUserClick = false;
            base.Localization(this);
            this.DTPermission = ServiceManager.Instance.PermissionAdminService.GetDT(base.UserInfo);
            this.DTPermission.DefaultView.Sort = BasePermissionTable.FieldSortCode;
            BaseBusinessLogic.SetFilter(this.DTPermission, BasePermissionTable.FieldEnabled, "1");
            this.DTPermission.AcceptChanges();
            this.BindData(true);
            this.SetControlState();
            this.IsUserClick = true;
        }

        private bool GetMultiSelect()
        {
            for (int i = 0; i < this.tvPermission.Nodes.Count; i++)
            {
                this.EntityToArray(this.tvPermission.Nodes[i]);
            }
            this.SelectedIDs = this.GetSelecteIDs();
            this.SelectedFullName = this.tvPermission.FocusedNode.GetValue(colTreelist).ToString();
            if ((this.OnButtonConfirmClick != null) && !this.OnButtonConfirmClick())
            {
                return false;
            }
            return true;
        }

        private string[] GetSelecteIDs()
        {
            string[] strArray = new string[0];
            if (this.SelecteTreeNodesID.Length > 1)
            {
                this.SelecteTreeNodesID = this.SelecteTreeNodesID.Substring(0, this.SelecteTreeNodesID.Length - 1);
                strArray = this.SelecteTreeNodesID.Split(new char[] { ',' });
            }
            return strArray;
        }

        private void GetSelecteTreeNodes(TreeNode TreeNode)
        {
            if ((TreeNode != null) && (TreeNode.Tag != null))
            {
                if (TreeNode.Checked)
                {
                    this.SelecteTreeNodesID = this.SelecteTreeNodesID + TreeNode.Tag.ToString() + ",";
                }
                for (int i = 0; i < TreeNode.Nodes.Count; i++)
                {
                    this.GetSelecteTreeNodes(TreeNode.Nodes[i]);
                }
            }
        }

        private bool GetSingleSelect()
        {
            this.SelectedID = this.tvPermission.FocusedNode.GetValue(colParentID).ToString();
            this.SelectedFullName = this.tvPermission.FocusedNode.GetValue(colTreelist).ToString();
            if (this.CheckMove && !this.CheckInputMove())
            {
                return false;
            }
            if ((this.OnButtonConfirmClick != null) && !this.OnButtonConfirmClick())
            {
                return false;
            }
            return true;
        }

        private void LoadTree()
        {
            this.tvPermission.BeginUpdate();
            this.tvPermission.Nodes.Clear();
            this.LoadTreePermission();
            BaseInterfaceLogic.FindTreeNode(this.tvPermission, this.OpenID, colParentID);
            if (BaseInterfaceLogic.TargetNode == null && this.tvPermission.Nodes.Count > 0)
            {
                BaseInterfaceLogic.TargetNode = this.tvPermission.Nodes[0];
            }
            if (BaseInterfaceLogic.TargetNode != null)
            {
                this.tvPermission.FocusedNode = BaseInterfaceLogic.TargetNode;
                BaseInterfaceLogic.ExpandTreeNode(this.tvPermission);
                this.tvPermission.FocusedNode.Expanded = true;
            }

            this.tvPermission.EndUpdate();
        }

        private void LoadTreePermission()
        {
            BaseInterfaceLogic.LoadTreeNodes(this.DTPermission, BasePermissionTable.FieldID, BasePermissionTable.FieldParentID, BasePermissionTable.FieldFullName, this.tvPermission);
        }

        private void SetControlState()
        {
            this.btnSetNull.Visible = this.AllowNull;
            if (this.DTPermission.Rows.Count > 0)
            {
                if (this.MultiSelect)
                {
                    this.btnSelectAll.Visible = true;
                    this.btnInvertSelect.Visible = true;
                }
                this.btnConfirm.Enabled = true;
            }
            if (this.DTPermission.Rows.Count == 0)
            {
                this.btnConfirm.Enabled = false;
            }
            else
            {
                this.btnConfirm.Enabled = true;
            }
            this.tvPermission.OptionsView.ShowCheckBoxes = this.MultiSelect;
        }

        private void SetTreeNodesSelected(TreeListNode TreeNode, bool selected)
        {
            if (TreeNode != null)
            {
                TreeNode.Checked = selected;
                for (int i = 0; i < TreeNode.Nodes.Count; i++)
                {
                    this.SetTreeNodesSelected(TreeNode.Nodes[i], selected);
                }
            }
        }

        private void SetTreeNodesTurnSelected(TreeListNode TreeNode)
        {
            if (TreeNode != null)
            {
                TreeNode.Checked = !TreeNode.Checked;
                for (int i = 0; i < TreeNode.Nodes.Count; i++)
                {
                    this.SetTreeNodesTurnSelected(TreeNode.Nodes[i]);
                }
            }
        }

        public bool AllowNull
        {
            get
            {
                return this.allowNull;
            }
            set
            {
                this.allowNull = value;
            }
        }

        public string ByPermission
        {
            get
            {
                return this.byPermission;
            }
            set
            {
                this.byPermission = value;
            }
        }

        public bool CheckMove
        {
            get
            {
                return this.checkMove;
            }
            set
            {
                this.checkMove = value;
            }
        }

        public string CurrentEntityID
        {
            get
            {
                var nID = this.tvPermission.FocusedNode.GetValue(colParentID);
                if ((this.tvPermission.FocusedNode != null) && (!string.IsNullOrEmpty(nID.ToString())))
                {
                    this.currentEntityID = nID.ToString();
                }
                return this.currentEntityID;
            }
            set
            {
                this.currentEntityID = value;
            }
        }

        public bool MultiSelect
        {
            get
            {
                return this.multiSelect;
            }
            set
            {
                this.multiSelect = value;
            }
        }

        public string OpenID
        {
            get
            {
                return this.openID;
            }
            set
            {
                this.openID = value;
            }
        }

        public string SelectedFullName
        {
            get
            {
                return this.selectedFullName;
            }
            set
            {
                this.selectedFullName = value;
            }
        }

        public string SelectedID
        {
            get
            {
                return this.selectedID;
            }
            set
            {
                this.selectedID = value;
            }
        }

        public string[] SelectedIDs
        {
            get
            {
                return this.selectedIDs;
            }
            set
            {
                this.selectedIDs = value;
            }
        }

        public delegate bool ButtonConfirmEventHandler();

        private void PermissionSelectFrm_Load(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            try
            {
                base.FormLoaded = false;
                this.FormOnLoad();
                base.FormLoaded = true;
            }
            catch (Exception exception)
            {
                base.ProcessException(exception);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void tvPermission_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
        {
            if (this.IsUserClick)
            {
                this.SetTreeNodesSelected(e.Node, e.Node.Checked);
            }
        }

        private void tvPermission_BeforeCheckNode(object sender, DevExpress.XtraTreeList.CheckNodeEventArgs e)
        {
            if (!this.IsUserClick)
            {
                e.State = e.PrevState;
            }
        }

        private void tvPermission_DoubleClick(object sender, EventArgs e)
        {
            this.btnConfirm.PerformClick();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            base.Close();
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (this.CheckInput())
            {
                bool multiSelect = false;
                if (this.MultiSelect)
                {
                    multiSelect = this.GetMultiSelect();
                }
                if (this.GetSingleSelect())
                {
                    base.DialogResult = System.Windows.Forms.DialogResult.OK;
                    base.Close();
                }
            }
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            this.IsUserClick = false;
            for (int i = 0; i < this.tvPermission.Nodes.Count; i++)
            {
                this.SetTreeNodesSelected(this.tvPermission.Nodes[i], true);
            }
            this.IsUserClick = true;
        }

        private void btnSetNull_Click(object sender, EventArgs e)
        {
            this.SelectedID = string.Empty;
            this.SelectedFullName = string.Empty;
            base.DialogResult = System.Windows.Forms.DialogResult.OK;
            base.Close();
        }

        private void btnTurnSelect_Click(object sender, EventArgs e)
        {
            this.SetTreeNodesTurnSelected(this.tvPermission.Nodes[0]);
        }

        private void btnInvertSelect_Click(object sender, EventArgs e)
        {
            this.SetTreeNodesTurnSelected(this.tvPermission.Nodes[0]);
        }

        private void tvPermission_NodeChanged(object sender, DevExpress.XtraTreeList.NodeChangedEventArgs e)
        {
            if (e.Node.HasChildren)
            {
                e.Node.ImageIndex = 1;
                e.Node.SelectImageIndex = 1;
            }
            else
            {
                e.Node.SelectImageIndex = 2;
            }
        }
    }
}